package com.project.slsx.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.adapter.FastPurchaseAdapter;
import com.project.slsx.adapter.TextAdapter;
import com.project.slsx.bean.FastPurchaseListBean;
import com.project.slsx.bean.PurchaseSupilerBean;
import com.project.slsx.utils.ParamsUtils;

import java.util.ArrayList;
import java.util.List;

public class FastPurchaseActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = FastPurchaseActivity.class.getSimpleName();
    private FastPurchaseAdapter adapter;
    private ArrayList<Boolean> categorySelected;
    private TextAdapter textadapter;
    private ArrayList<String> ids;
    private String categoryId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_purchase_fast;
    }

    @Override
    public void initView() {
        TextView title = findViewById(R.id.tv_title);
        title.setText("快速采购");
        findViewById(R.id.rl_back).setOnClickListener(v -> finish());
        RecyclerView rvCategory = findViewById(R.id.recyclerview_purchase_category);
        rvCategory.setLayoutManager(new LinearLayoutManager(this));
        textadapter = new TextAdapter(R.layout.layout_item_title);
        rvCategory.setAdapter(textadapter);

        RecyclerView recyclerView = findViewById(R.id.recyclerview_purchase);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new FastPurchaseAdapter(R.layout.layout_item_fast_purchase);
        recyclerView.setAdapter(adapter);

    }

    @Override
    public void initData() {
        categorySelected = new ArrayList<>();
        ids = new ArrayList<>();//供应商id
        //获取供应商列表
        HttpParams purchasesupplierlistParams = ParamsUtils.getPurchasesupplierlistParams();
        purchasesupplierlistParams.put("is_fast", "1");
        getPurchaseSupplierList(purchasesupplierlistParams);

        textadapter.setOnItemClickListener((adapter, view, position) -> {
            Log.i(TAG, "initData: ");

            ArrayList<Boolean> selected = textadapter.getSelected();
            for (int i = 0; i < selected.size(); i++) {
                if (i == position) {
                    selected.set(i, true);
                } else {
                    selected.set(i, false);
                }
            }
            textadapter.setSelected(selected);

            getFastPurchaseDataList(ids.get(position));

        });
    }

    private void getPurchaseSupplierList(HttpParams purchasesupplierlistParams) {
        OkGo.<String>post(Constans.BASEURL)
                .tag("is_fast").params(purchasesupplierlistParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                PurchaseSupilerBean purchaseSupilerBean = JSONObject.parseObject(response.body(), PurchaseSupilerBean.class);
                showToast(mContext, purchaseSupilerBean.getErrmsg());
                if (purchaseSupilerBean.getErrno() == Constans.SUCCESS) {
                    if (purchaseSupilerBean.getResult() != null && purchaseSupilerBean.getResult().size() > 0) {

                        int size = purchaseSupilerBean.getResult().size();
                        for (int i = 0; i < size; i++) {
                            if (i == 0) {
                                categorySelected.add(true);
                            } else {
                                categorySelected.add(false);
                            }
                        }
                        textadapter.setSelected(categorySelected);

                        ArrayList<String> namses = new ArrayList<>();
                        for (int i = 0; i < purchaseSupilerBean.getResult().size(); i++) {
                            namses.add(purchaseSupilerBean.getResult().get(i).getName());
                            ids.add(purchaseSupilerBean.getResult().get(i).getId());
                        }
                        textadapter.setNewData(namses);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                Log.i(TAG, "onError: " + response.toString());
                showToast(mContext, "网络错误");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                Log.i(TAG, "onFinish: ");
                if (ids != null && ids.size() > 0) {
                    getFastPurchaseDataList(ids.get(0));
                }
            }
        });
    }


    /**
     * 获取商品
     *
     * @param id
     */
    private void getFastPurchaseDataList(String id) {
        HttpParams purchaselistParams = ParamsUtils.getPurchaselistParams();
        purchaselistParams.put("is_fast", "1");
        purchaselistParams.put("supplier_id", id);
        OkGo.<String>post(Constans.BASEURL).tag("list").params(purchaselistParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                FastPurchaseListBean fastPurchaseListBean = JSONObject.parseObject(response.body(), FastPurchaseListBean.class);
                if (fastPurchaseListBean.getErrno() == Constans.SUCCESS) {
                    String amount = fastPurchaseListBean.getAmount();
                    String number = fastPurchaseListBean.getNumber();
                    List<FastPurchaseListBean.ResultBean> result = fastPurchaseListBean.getResult();

                    if (result != null && result.size() > 0) {
                        adapter.setNewData(result);
                    }
                } else {
                    showToast(mContext, "暂无数据");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);

                showToast(mContext, "网络错误");

            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        });
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.t_cash_category_all:
                categoryId = "";
                break;
        }
    }
}
